package com.david.flowable.controller;

import com.david.flowable.pojo.vo.FlowSaveXmlVO;
import com.david.flowable.pojo.vo.FormFlowVO;
import com.david.flowable.service.FlowDefinitionService;
import com.david.system.pojo.Entity.JobEntity;
import com.david.system.pojo.Entity.UserEntity;
import com.david.system.pojo.Entity.UserInfo;
import lombok.Getter;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

import static com.david.common.utils.ResultUtils.success;

@RestController
@RequestMapping("/flowable/definition")
@Slf4j
public class FlowDefinitionController {

    @Autowired
    private FlowDefinitionService flowDefinitionService;
    /**
    * @Description: 保存上传的流程信息，格式XML
    * @Param: [flowSaveXmlVO]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/8/13
    */

    @PostMapping("/save")
    public ResponseEntity<Object> save(@RequestBody FlowSaveXmlVO flowSaveXmlVO){
        log.info("保存上传的xml：{}", flowSaveXmlVO);
        InputStream in = null;
        in = new ByteArrayInputStream(flowSaveXmlVO.getXml().getBytes(StandardCharsets.UTF_8));
        String flowDefId = flowDefinitionService.saveXml(flowSaveXmlVO.getName(),flowSaveXmlVO.getCategory(),in);
        return success(true, flowDefId);
    }

    /**
    * @Description: 根据Ids查询用户
    * @Param: [ids]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/8/9
    */
    @PostMapping("/users/{ids}")
    public ResponseEntity<Object> users(@PathVariable List<Long> ids){
        log.info("查询用户{}", ids);
        List<UserInfo> users = flowDefinitionService.searchUsers(ids);
        return success(true, users);
    }
    /**
    * @Description: 根据Ids查询岗位
    * @Param: [ids]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/8/9
    */
    @PostMapping("/jobs/{ids}")
    public ResponseEntity<Object> jobs(@PathVariable List<Integer> ids){
        log.info("查询岗位{}",ids);
        List<JobEntity> jobs = flowDefinitionService.searchJobs(ids);
        return success(true, jobs);
    }

    /**
    * @Description: 获取审批流程信息
    * @Param: [deployId, proInsId]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/8/13
    */
    @GetMapping("/XmlAndNode")
    public ResponseEntity<Object> getXmlAndNode(@RequestParam("deployId") String deployId, @RequestParam(required = false, value = "proInsId") String proInsId) {
        log.info("查询流程xml{},{}",deployId, proInsId);
        Map<String, Object> xmlAndNode = flowDefinitionService.getXmlAndNode(deployId,proInsId);
        return success(true, xmlAndNode);
    }

    @GetMapping("/xml")
    public ResponseEntity<Object> getXml(@RequestParam("flowId") String flowId) {
        log.info("查询流程xml{}",flowId);
        Map<String, Object> xmlWithoutNode = flowDefinitionService.getXml(flowId);
        return success(true, xmlWithoutNode);
    }


}
